#include <iostream>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
using namespace log4cpp;
using std::cout;
using std::endl;

void test0(){
    //1.设置日志布局
    PatternLayout * ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn3 = new PatternLayout();
    ptn3->setConversionPattern("%d %c [%p] %m%n");

    //2.创建目的地对象
    OstreamAppender * pos = new OstreamAppender("console",&cout);
    pos->setLayout(ptn1);

    auto pfile = new FileAppender("fileApp","wd.log");
    pfile->setLayout(ptn2);

    auto rollingFileApp = new RollingFileAppender("rollingFile",
                                                  "rollingwd.log",
                                                  5 * 1024,
                                                  9);
    rollingFileApp->setLayout(ptn3);


    //3.创建日志记录器
    Category & salesDepart = Category::getInstance("salesDepart");
    //3.1 设置日志系统优先级
    salesDepart.setPriority(Priority::DEBUG);
    //3.2 设置日志目的地
    salesDepart.addAppender(pos);
    salesDepart.addAppender(pfile);
    salesDepart.addAppender(rollingFileApp);


    //4.记录日志
    int count = 100;
    while(count-- > 0){
        salesDepart.emerg("this is an emerg msg");
        salesDepart.fatal("this is a fatal msg");
        salesDepart.alert("this is an alert msg");
        salesDepart.crit("this is a crit msg");
        salesDepart.error("this is an error msg");
        salesDepart.warn("this is a warn msg");
        salesDepart.notice("this is a notice msg");
        salesDepart.info("this is an info msg");
        salesDepart.debug("this is a debug msg");
    }

    //5.回收资源
    Category::shutdown();
}

int main(void){
    test0();
    return 0;
}

